﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 数据函数库;
using WFNetLib.TCP;

namespace 温室大棚
{
    public partial class Form实时数据 : Form
    {
        public Form实时数据()
        {
            InitializeComponent();
        }
        public void tcpAsyncServer_ReceiveServerEvent(object sender, ReceiveServerEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                try
                {
                    TcpPacket[] rxList = (TcpPacket[])e.netPacket;
                    foreach (TcpPacket rx in rxList)
                    {
                        switch (rx.Header.Command)
                        {
                            case 1:
                                实时数据Data ssData = DataAnalysis.实时数据DataAnalysis(rx);                                
                                if (bStart)
                                {
                                    if (ssData.GreenhouseAddr == GreenhouseAddr && ssData.NoteNum == NoteNum)
                                    {
                                        RxCount++;
                                        toolStripTextBox3.Text = RxCount.ToString();
                                        if (cgqData != null)
                                        {
                                            ShowData(ssData.空气温度1, 1, cgqData.空气温度下限, cgqData.空气温度上限, textBox1);
                                            ShowData(ssData.空气温度2, 2, cgqData.空气温度下限, cgqData.空气温度上限, textBox2);
                                            ShowData(ssData.空气温度3, 3, cgqData.空气温度下限, cgqData.空气温度上限, textBox3);

                                            ShowData(ssData.土壤温度1, 1, cgqData.土壤温度下限, cgqData.土壤温度上限, textBox6);
                                            ShowData(ssData.土壤温度2, 2, cgqData.土壤温度下限, cgqData.土壤温度上限, textBox5);
                                            ShowData(ssData.土壤温度3, 3, cgqData.土壤温度下限, cgqData.土壤温度上限, textBox4);

                                            ShowData(ssData.空气湿度1, 1, cgqData.空气湿度下限, cgqData.空气湿度上限, textBox9);
                                            ShowData(ssData.空气湿度2, 2, cgqData.空气湿度下限, cgqData.空气湿度上限, textBox8);
                                            ShowData(ssData.空气湿度3, 3, cgqData.空气湿度下限, cgqData.空气湿度上限, textBox7);

                                            ShowData(ssData.土壤湿度1, 1, cgqData.土壤湿度下限, cgqData.土壤湿度上限, textBox12);
                                            ShowData(ssData.土壤湿度2, 2, cgqData.土壤湿度下限, cgqData.土壤湿度上限, textBox11);
                                            ShowData(ssData.土壤湿度3, 3, cgqData.土壤湿度下限, cgqData.土壤湿度上限, textBox10);

                                            ShowData(ssData.光照1, 1, cgqData.光照下限, cgqData.光照上限, textBox15);
                                            ShowData(ssData.光照2, 2, cgqData.光照下限, cgqData.光照上限, textBox14);
                                            ShowData(ssData.光照3, 3, cgqData.光照下限, cgqData.光照上限, textBox13);

                                            ShowData(ssData.二氧化碳1, 1, cgqData.二氧化碳下限, cgqData.二氧化碳上限, textBox18);
                                            ShowData(ssData.二氧化碳2, 2, cgqData.二氧化碳下限, cgqData.二氧化碳上限, textBox17);
                                            ShowData(ssData.二氧化碳3, 3, cgqData.二氧化碳下限, cgqData.二氧化碳上限, textBox16);

                                            ShowData(ssData.土壤盐分1, 1, cgqData.土壤盐分下限, cgqData.土壤盐分上限, textBox21);
                                            ShowData(ssData.土壤盐分2, 2, cgqData.土壤盐分下限, cgqData.土壤盐分上限, textBox20);
                                            ShowData(ssData.土壤盐分3, 3, cgqData.土壤盐分下限, cgqData.土壤盐分上限, textBox19);
                                        }
                                        else
                                        {
                                            ShowData(ssData.空气温度1, 1,0, 0, textBox1);
                                            ShowData(ssData.空气温度2, 2,0,0, textBox2);
                                            ShowData(ssData.空气温度3, 3,0, 0, textBox3);

                                            ShowData(ssData.土壤温度1, 1, 0,0, textBox6);
                                            ShowData(ssData.土壤温度2, 2, 0,0, textBox5);
                                            ShowData(ssData.土壤温度3, 3, 0,0, textBox4);

                                            ShowData(ssData.空气湿度1,0,0, 1,  textBox9);
                                            ShowData(ssData.空气湿度2, 2, 0,0, textBox8);
                                            ShowData(ssData.空气湿度3, 3, 0,0, textBox7);

                                            ShowData(ssData.土壤湿度1, 1, 0,0, textBox12);
                                            ShowData(ssData.土壤湿度2, 2, 0,0, textBox11);
                                            ShowData(ssData.土壤湿度3, 3, 0,0, textBox10);

                                            ShowData(ssData.光照1, 1, 0,0, textBox15);
                                            ShowData(ssData.光照2, 2, 0,0, textBox14);
                                            ShowData(ssData.光照3, 3, 0,0, textBox13);

                                            ShowData(ssData.二氧化碳1, 1, 0,0, textBox18);
                                            ShowData(ssData.二氧化碳2, 2, 0,0, textBox17);
                                            ShowData(ssData.二氧化碳3, 3, 0,0, textBox16);

                                            ShowData(ssData.土壤盐分1, 1, 0,0, textBox21);
                                            ShowData(ssData.土壤盐分2, 2, 0,0, textBox20);
                                            ShowData(ssData.土壤盐分3, 3, 0,0, textBox19);
                                        }
                                    }
                                }                                
                                break;
                            case 2:
                                大棚信息Data dpData1 = DataAnalysis.大棚信息DataAnalysis(rx);
                                if (bStart)
                                {
                                    if (dpData1.GreenhouseAddr == GreenhouseAddr && dpData1.NoteNum == NoteNum)
                                    {
                                        dpData = dpData1;
                                    }
                                }                                                           
                                break;
                            case 3:
                                传感器阈值Data cgqData1 = DataAnalysis.传感器阈值DataAnalysis(rx);
                                if (bStart)
                                {
                                    if (cgqData1.GreenhouseAddr == GreenhouseAddr && cgqData1.NoteNum == NoteNum)
                                    {
                                        cgqData = cgqData1;
                                    }
                                }     
                                break;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    //textBox1.AppendText("发生错误" + ex.Message);
                }  
            }));
        }

        private void Form实时数据_Load(object sender, EventArgs e)
        {

        }
        Int32 GreenhouseAddr;
        Int32 NoteNum;
        bool bStart;
        int RxCount;
        大棚信息Data dpData;
        传感器阈值Data cgqData;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
            if (toolStripButton1.Text == "开始监测")
            {
                toolStripButton1.Text = "停止检测";                
                if (!Int32.TryParse(toolStripTextBox1.Text, out GreenhouseAddr))
                {
                    MessageBox.Show("大棚号请输入正确的数值!");
                    return;
                }                
                if (!Int32.TryParse(toolStripTextBox2.Text, out NoteNum))
                {
                    MessageBox.Show("节点号请输入正确的数值!");
                    return;
                }
                DataTable dt = 大棚信息DataDBOption.DataTableSelect(GreenhouseAddr,NoteNum);
                if (dt.Rows.Count == 0)
                {
                    dpData = null;
                }
                else 
                    dpData = 大棚信息DataFactory.Construct(dt.Rows[0]);

                dt = 传感器阈值DataDBOption.DataTableSelect(GreenhouseAddr, NoteNum);
                if (dt.Rows.Count == 0)
                {
                    cgqData = null;
                }
                else 
                    cgqData = 传感器阈值DataFactory.Construct(dt.Rows[0]);
                SetCGQ();

                toolStripTextBox1.Enabled = false;
                toolStripTextBox2.Enabled = false;
                bStart = true;
                RxCount = 0;
                toolStripTextBox3.Text = RxCount.ToString();
            }
            else
            {
                toolStripButton1.Text = "开始监测";
                toolStripTextBox1.Enabled = true;
                toolStripTextBox2.Enabled = true;
                bStart = false;
            }
        }
        void SetDP()
        {
            switch (dpData.空气温度数量)
            {
                case 0:
                    textBox1.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox2.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox3.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox25.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox1.BackColor = System.Drawing.SystemColors.Control;
                    textBox2.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox3.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox25.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox1.BackColor = System.Drawing.SystemColors.Control;
                    textBox2.BackColor = System.Drawing.SystemColors.Control;
                    textBox3.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox25.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox1.BackColor = System.Drawing.SystemColors.Control;
                    textBox2.BackColor = System.Drawing.SystemColors.Control;
                    textBox3.BackColor = System.Drawing.SystemColors.Control;
                    textBox25.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.土壤温度数量)
            {
                case 0:
                    textBox6.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox5.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox4.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox28.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox6.BackColor = System.Drawing.SystemColors.Control;
                    textBox5.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox4.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox28.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox6.BackColor = System.Drawing.SystemColors.Control;
                    textBox5.BackColor = System.Drawing.SystemColors.Control;
                    textBox4.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox28.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox6.BackColor = System.Drawing.SystemColors.Control;
                    textBox5.BackColor = System.Drawing.SystemColors.Control;
                    textBox4.BackColor = System.Drawing.SystemColors.Control;
                    textBox28.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.空气湿度数量)
            {
                case 0:
                    textBox9.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox8.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox7.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox27.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox9.BackColor = System.Drawing.SystemColors.Control;
                    textBox8.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox7.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox27.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox9.BackColor = System.Drawing.SystemColors.Control;
                    textBox8.BackColor = System.Drawing.SystemColors.Control;
                    textBox7.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox27.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox9.BackColor = System.Drawing.SystemColors.Control;
                    textBox8.BackColor = System.Drawing.SystemColors.Control;
                    textBox7.BackColor = System.Drawing.SystemColors.Control;
                    textBox27.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.土壤湿度数量)
            {
                case 0:
                    textBox12.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox11.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox10.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox26.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox12.BackColor = System.Drawing.SystemColors.Control;
                    textBox11.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox10.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox26.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox12.BackColor = System.Drawing.SystemColors.Control;
                    textBox11.BackColor = System.Drawing.SystemColors.Control;
                    textBox10.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox26.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox12.BackColor = System.Drawing.SystemColors.Control;
                    textBox11.BackColor = System.Drawing.SystemColors.Control;
                    textBox10.BackColor = System.Drawing.SystemColors.Control;
                    textBox26.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.光照数量)
            {
                case 0:
                    textBox15.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox14.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox13.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox29.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox15.BackColor = System.Drawing.SystemColors.Control;
                    textBox14.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox13.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox29.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox15.BackColor = System.Drawing.SystemColors.Control;
                    textBox14.BackColor = System.Drawing.SystemColors.Control;
                    textBox13.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox29.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox15.BackColor = System.Drawing.SystemColors.Control;
                    textBox14.BackColor = System.Drawing.SystemColors.Control;
                    textBox13.BackColor = System.Drawing.SystemColors.Control;
                    textBox29.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.二氧化碳数量)
            {
                case 0:
                    textBox18.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox17.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox16.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox32.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox18.BackColor = System.Drawing.SystemColors.Control;
                    textBox17.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox16.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox32.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox18.BackColor = System.Drawing.SystemColors.Control;
                    textBox17.BackColor = System.Drawing.SystemColors.Control;
                    textBox16.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox32.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox18.BackColor = System.Drawing.SystemColors.Control;
                    textBox17.BackColor = System.Drawing.SystemColors.Control;
                    textBox16.BackColor = System.Drawing.SystemColors.Control;
                    textBox32.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
            switch (dpData.土壤盐分数量)
            {
                case 0:
                    textBox21.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox20.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox19.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox31.BackColor = System.Drawing.SystemColors.WindowText;
                    break;
                case 1:
                    textBox21.BackColor = System.Drawing.SystemColors.Control;
                    textBox20.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox19.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox31.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 2:
                    textBox21.BackColor = System.Drawing.SystemColors.Control;
                    textBox20.BackColor = System.Drawing.SystemColors.Control;
                    textBox19.BackColor = System.Drawing.SystemColors.WindowText;
                    textBox31.BackColor = System.Drawing.SystemColors.Control;
                    break;
                case 3:
                    textBox21.BackColor = System.Drawing.SystemColors.Control;
                    textBox20.BackColor = System.Drawing.SystemColors.Control;
                    textBox19.BackColor = System.Drawing.SystemColors.Control;
                    textBox31.BackColor = System.Drawing.SystemColors.Control;
                    break;
            }
        }
        void SetCGQ()
        {
            if (cgqData != null)
            {
                textBox25.Text=cgqData.空气温度下限.ToString() + "/" + cgqData.空气温度上限.ToString();
                textBox28.Text = cgqData.土壤温度下限.ToString() + "/" + cgqData.土壤温度上限.ToString();
                textBox27.Text = cgqData.空气湿度下限.ToString() + "/" + cgqData.空气湿度上限.ToString();
                textBox26.Text = cgqData.土壤湿度下限.ToString() + "/" + cgqData.土壤湿度上限.ToString();
                textBox29.Text = cgqData.光照下限.ToString() + "/" + cgqData.光照上限.ToString();
                textBox32.Text = cgqData.二氧化碳下限.ToString() + "/" + cgqData.二氧化碳上限.ToString();
                textBox31.Text = cgqData.土壤盐分下限.ToString() + "/" + cgqData.土壤盐分上限.ToString();
            }
        }
        void ShowData(float data,int index,int low,int hight,TextBox tb)
        {
            if (data == -1)
            {
                if (dpData == null)
                {
                    tb.Text = "传感器故障";
                }
                else
                {
                    if (dpData.空气温度数量 >= index)
                        tb.Text = "传感器故障";
                    else
                        tb.Text = "-.-.-.-.-.-.-.-.-.-.-";
                }
            }
            else
            {
                if (dpData.空气温度数量 >= index)
                {
                    tb.Text = data.ToString();
                    if (cgqData != null)
                    {
                        if (data < low)
                        {
                            tb.ForeColor = System.Drawing.Color.Blue;
                        }
                        else if (data > hight)
                        {
                            tb.ForeColor = System.Drawing.Color.Red;
                        }
                        else
                        {
                            tb.ForeColor = System.Drawing.SystemColors.WindowText;
                        }
                    }
                    else
                        tb.ForeColor = System.Drawing.SystemColors.WindowText;                    
                }
                else
                {
                    tb.Text = "-.-.-.-.-.-.-.-.-.-.-";
                    tb.ForeColor = System.Drawing.SystemColors.WindowText;
                }
            } 
        }
    }
}
